/**
 * @author Christopher
 */

var consoleQueue = new Array();
var money = 100;
var rating = 5;
var debug = 1;

function init() {

	generateEmployee(5);

	//load employees
	objx(employees).each(function(item) {
		var x = document.getElementById("emplyselect");
		var option = document.createElement("option");
		option.text = item.Name;
		x.add(option);
		document.getElementById("name").value = item.Name;
		document.getElementById("loyalty").value = item.Loyalty;
		document.getElementById("efficiency").value = item.Efficiency;
		document.getElementById("emplyselect").selectedIndex = employees.length - 1;

	});

	populateSuppliers();
	populateMenuList();
	document.getElementById("employeewage").value = wage;

}

function employeenumchange() {
	numemployees = document.getElementById("numemployee").value;
}

function wagechange() {
	wage = document.getElementById("employeewage").value;
}

function refresheventTextArea() {
	var text = "";
	for ( i = 0; i < activeEventsQueue.length; i++) {

		text = text + activeEventsQueue[i] + "\n";

	}
	document.getElementById("eventtextarea").value = text;
}

function selectchange() {
	supplier = document.getElementById("supselect").value;
	objx(suppliers).each(function(item) {
		if (item.Name == supplier) {
			supplymultiplier = item.Multiplier;
		}
	});

}

function menuchange() {

	var x = document.getElementById("menuselect").value;
	if (selectedMenuQueue.indexOf(x) == -1) {
		selectedMenuQueue.push(x);
		refreshselectedmenu();
	}
}

function refreshselectedmenu() {

	var x = document.getElementById("currentmenu").options.length = 0;

	var y = document.getElementById("currentmenu");
	for (var v = 0; v < selectedMenuQueue.length; v++) {
		var option = document.createElement("option");
		option.text = selectedMenuQueue[v];
		y.add(option);
	}
}

function menuremove() {
	var i = selectedMenuQueue.indexOf(document.getElementById("currentmenu").value);
	if (i != -1) {

		selectedMenuQueue.splice(i, 1);
		refreshselectedmenu();
	}
}

function populateMenuList() {

	for ( i = 0; i < menuQueue.length; i++) {
		var x = document.getElementById("menuselect");
		var option = document.createElement("option");
		option.text = menuQueue[i];
		x.add(option);

	}

}

function updateMoney() {
	document.getElementById("money").value = "$" + Math.floor(money);
}

// misc functions

function writeToConsole(string) {
	if (consoleQueue.length > 20) {
		consoleQueue.length = 0;
	}
	document.getElementById("textarea").value = "";
	//clear the console
	consoleQueue.unshift(string);
	//add the string to the top of the console queue
	for (var i = 0; i < consoleQueue.length; i++) {
		if (document.getElementById("textarea").value == "") {
			document.getElementById("textarea").value = consoleQueue[i];
		} else {
			var consoletext = document.getElementById("textarea").value;
			document.getElementById("textarea").value = consoletext + "\n" + consoleQueue[i];
		}
	}

}

function notify(text, type) {
	if (type != null) {
		$.notify(text, type);
	} else {
		$.notify(text, "base");
	}
}

function randomIntFromInterval(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}

function guid() {
	function s4() {
		return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
	}

	return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4();
}

function getDate() {

	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var second = now.getSeconds();
	var ap = "AM";
	if (hour > 11) {
		ap = "PM";
	}
	if (hour > 12) {
		hour = hour - 12;
	}
	if (hour == 0) {
		hour = 12;
	}
	if (hour < 10) {
		hour = "0" + hour;
	}
	if (minute < 10) {
		minute = "0" + minute;
	}
	if (second < 10) {
		second = "0" + second;
	}
	var timeString = hour + ':' + minute + ':' + second + " " + ap;
	return timeString;
}